<?php

namespace App\Http\Controllers;

use App\Http\Requests\CreateTempRequest;
use App\Http\Requests\UpdateTempRequest;
use App\Models\Questions;
use App\Repositories\TempRepository;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Facades\Input;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class TempController extends AppBaseController
{
    /** @var  TempRepository */
    private $tempRepository;

    public function __construct(TempRepository $tempRepo)
    {
        $this->tempRepository = $tempRepo;
    }

    /**
     * Display a listing of the Temp.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
    	$limit = $request->get('limit', 10);
	    $this->tempRepository->pushCriteria(new RequestCriteria($request));
	    $temps = $this->tempRepository->paginate($limit);

        return view('temps.index')
            ->with('temps', $temps);
    }

    /**
     * Show the form for creating a new Temp.
     *
     * @return Response
     */
    public function create()
    {
        return view('temps.create');
    }

    /**
     * Store a newly created Temp in storage.
     *
     * @param CreateTempRequest $request
     *
     * @return Response
     */
    public function store(CreateTempRequest $request)
    {
        $input = $request->all();

	    $temp = $this->tempRepository->create($input);

        Flash::success('Temp saved successfully.');

        return redirect(route('temps.index'));
    }

    /**
     * Display the specified Temp.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $temp = $this->tempRepository->findWithoutFail($id);

        if (empty($temp)) {
            Flash::error('Temp not found');

            return redirect(route('temps.index'));
        }

        $questions = $temp->questions()->paginate(Input::get('limit', 10));
        $type = Questions::getType();

        return view('questions.index', compact('questions', 'id', 'type'));
    }

    /**
     * Show the form for editing the specified Temp.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $temp = $this->tempRepository->findWithoutFail($id);

        if (empty($temp)) {
            Flash::error('Temp not found');

            return redirect(route('temps.index'));
        }

        return view('temps.edit')->with('temp', $temp);
    }

    /**
     * Update the specified Temp in storage.
     *
     * @param  int              $id
     * @param UpdateTempRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateTempRequest $request)
    {
        $temp = $this->tempRepository->findWithoutFail($id);

        if (empty($temp)) {
            Flash::error('Temp not found');

            return redirect(route('temps.index'));
        }

        $temp = $this->tempRepository->update($request->all(), $id);

        Flash::success('Temp updated successfully.');

        return redirect(route('temps.index'));
    }

    /**
     * Remove the specified Temp from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $temp = $this->tempRepository->findWithoutFail($id);

        if (empty($temp)) {
            Flash::error('Temp not found');

            return redirect(route('temps.index'));
        }

        $this->tempRepository->delete($id);

        Flash::success('Temp deleted successfully.');

        return redirect(route('temps.index'));
    }
}
